import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { resourceType } = query;

  // 事件定义数据
  const eventDefs = [
    {
      name: 'RoomReserved',
      resourceType: 'room',
      payloadSchema: {
        type: 'object',
        properties: {
          roomId: { type: 'string' },
          reservationId: { type: 'string' },
          guestName: { type: 'string' },
          checkInDate: { type: 'string' },
          checkOutDate: { type: 'string' },
          reservedAt: { type: 'string' },
        },
        required: ['roomId', 'reservationId', 'guestName'],
      },
      channel: 'room-events',
    },
    {
      name: 'RoomCheckedIn',
      resourceType: 'room',
      payloadSchema: {
        type: 'object',
        properties: {
          roomId: { type: 'string' },
          checkInId: { type: 'string' },
          guestId: { type: 'string' },
          checkInTime: { type: 'string' },
          roomKey: { type: 'string' },
        },
        required: ['roomId', 'checkInId', 'guestId'],
      },
      channel: 'room-events',
    },
    {
      name: 'RoomCheckedOut',
      resourceType: 'room',
      payloadSchema: {
        type: 'object',
        properties: {
          roomId: { type: 'string' },
          checkoutId: { type: 'string' },
          checkOutTime: { type: 'string' },
          totalAmount: { type: 'number' },
          paymentMethod: { type: 'string' },
        },
        required: ['roomId', 'checkoutId'],
      },
      channel: 'room-events',
    },
    {
      name: 'RoomCleaningStarted',
      resourceType: 'room',
      payloadSchema: {
        type: 'object',
        properties: {
          roomId: { type: 'string' },
          cleaningId: { type: 'string' },
          assignedTo: { type: 'string' },
          startedAt: { type: 'string' },
          estimatedDuration: { type: 'number' },
        },
        required: ['roomId', 'cleaningId', 'assignedTo'],
      },
      channel: 'housekeeping-events',
    },
    {
      name: 'RoomCleaningCompleted',
      resourceType: 'room',
      payloadSchema: {
        type: 'object',
        properties: {
          roomId: { type: 'string' },
          cleaningId: { type: 'string' },
          completedAt: { type: 'string' },
          qualityScore: { type: 'number' },
          notes: { type: 'string' },
        },
        required: ['roomId', 'cleaningId'],
      },
      channel: 'housekeeping-events',
    },
  ];

  // 按资源类型过滤
  let filteredEvents = eventDefs;
  if (resourceType) {
    filteredEvents = eventDefs.filter(
      (event) => event.resourceType === resourceType,
    );
  }

  return {
    code: 0,
    data: filteredEvents,
    message: '获取事件定义成功',
  };
});
